import * as utils from '../../utils/util.js';
//获取应用实例
const app = getApp()
const imgurl = '../../images/myquired.png';
Page({
  data: {
    phone:'',
    name: '',
    headerImg:'',
    babyList:[],
    isLogin:false,
    mask_view: true, //遮罩层默认不显示
    iHomeDialog: true //iHome弹框默认不显示
  },
  toLogin(){
    //去登陆
    wx.navigateTo({
      url: '/pages/login/login'
    })
  },
  //我的资料
  toMyInfo(){
    wx.navigateTo({
      url: '/pages/myInfo/myInfo'
    })
  },
  //我的宝宝
  openMyBaby() {
    // wx.navigateTo({
    //   url: '/pages/myBaby/myBaby'
    // })
    let url='https://hengqinlife-uat.e-hqins.com/weshop/productDetailNew?productId=10001171403&channelCode=10002&promoteInstanceCode=INST1576050024461&redirectType=1&goodsCode=PG1576049324493&agent=WX00000005&thirdChannelCode='
    wx.navigateTo({
      url: '/pages/webView/webView?url='+encodeURIComponent(url) 
    })
  },
  //亲友团
  openBridalParty() {
    let that = this;
    //检测是否登录
    if (that.data.isLogin) {
      wx.navigateTo({
        url: '/pages/bridalParty/bridalParty',
      })
    } else {
      wx.showModal({
        title: '系统提示',
        content: '您还未登录,是否去登录',
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login'
            })
          } else if (res.cancel) {
            that.setData({
              isLogin: false
            })
          }
        }
      })
    }
  },
  //成长记录
  openGrowthRecord() {
    let that = this;
    let babyList = that.data.babyList;
    if (babyList.length <= 0){
      wx.showModal({
        title: '系统提示',
        content: '您未添加宝宝，请添加宝宝',
        success:function(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/addBaby/addBaby',
            })
          } else if (res.cancel) {
            return
          }
        }
      })
    }else{
      //检测是否登录
      if (this.data.token && this.data.token != "") {
        wx.navigateTo({
          url: '/pages/growthRecord/growthRecord?active=1',
        })
      }
    }
  },
  //接种单位查询
  openUnitSearch() {
    wx.navigateTo({
      url: '/pages/inoculationUnit/inoculationUnit'
    })
  },
  //建议反馈
  openFeedback() {
    wx.navigateTo({
      url: '/pages/feedback/feedback'
    })
  },
  //我的保险
  openInsure() {
    let that = this;
    let babyList = that.data.babyList;
    if (babyList.length <= 0) {
      wx.showModal({
        title: '系统提示',
        content: '您未添加宝宝，请添加宝宝',
        success: function (res) {
          if (res.confirm) {
            console.log(res)
            wx.navigateTo({
              url: '/pages/addBaby/addBaby',
            })
          } else if (res.cancel) {
            return
          }
        }
      })
    } else {
      //检测是否登录
      if (this.data.token && this.data.token != "") {
        wx.navigateTo({
          url: '/pages/insureDeail/insureDeail',
        })
      }
    }
  },
  //设置
  openSetUp() {
    wx.navigateTo({
      url: '/pages/setUp/setUp'
    })
  },
  //iHOME
  openIHome() {
    this.setData({
      mask_view: false,
      iHomeDialog: false
    })
  },
  //关闭iHome
  closeLogin() {
    this.setData({
      mask_view: true,
      iHomeDialog: true
    })
  },
  chooseImage(){
    wx.chooseImage({
      count:1,
      sourceType: ['album', 'camera'],
      success: function(res) {
        if (res.tempFilePaths){
          const imagePath = res.tempFilePaths[0];
          wx.navigateTo({
            url: '/pages/wx-cropping/wx-cropping',
            success(res) {
              // 通过eventChannel向被打开页面传送数据
              //发送选择的图片地址
              res.eventChannel.emit('postImageurl', { data: imagePath })
            }
          })
        }
      },
    })
  },
  getBabyList() {
    utils.HttpRequst(false, "/baby-cloud-base/mobileBabyInfo/listAll", "post", {}, (res) => {
      if (res.data.code == 200) {
        this.setData({
          babyList:res.data.data
        })
      }

    })
  },
  //保存图片
  saveIHome() {
    new Promise((resolve,reject)=>{
      wx.showLoading({
        title: '保存中...'
      })
      wx.downloadFile({
        url: 'https://hq-prd-ymxt.oss-cn-szfinance.aliyuncs.com/upload/store/pt/2020/04/903585d4-362a-4ec3-8b4d-ee6fd3cea1c1.png?x-oss-process',
        success(res) {
          var benUrl = res.tempFilePath;
          console.log(benUrl)
          //图片保存到本地相册
          wx.saveImageToPhotosAlbum({
            filePath: benUrl,
            //授权成功，保存图片
            success: function (data) {
              wx.showToast({
                title: '保存成功',
                icon: 'success',
                duration: 2000
              })
            },
            //授权失败
            fail(err) {
              if (err.errMsg) {//重新授权弹框确认
                  wx.showModal({
                    title: '提示',
                    content: '请先授权，再保存此图片。',
                    success(res) {
                      if (res.confirm) {//重新授权弹框用户点击了确定
                        wx.openSetting({//进入小程序授权设置页面
                          success(settingdata) {
                            console.log(settingdata)
                            if (settingdata.authSetting['scope.writePhotosAlbum']) {//用户打开了保存图片授权开关
                              wx.saveImageToPhotosAlbum({
                                filePath: benUrl,
                                success: function (data) {
                                  wx.showToast({
                                    title: '保存成功',
                                    icon: 'success',
                                    duration: 2000
                                  })
                                },
                              })
                            } else {//用户未打开保存图片到相册的授权开关
                              wx.showModal({
                                title: '温馨提示',
                                content: '授权失败，请稍后重新获取',
                                showCancel: false,
                              })
                            }
                          }
                        })
                      }
                    }
                  })
              }
            },
            complete(res) {
              wx.hideLoading()
            }
          })
        }
      })

    }).then(res=>{
      this.setData({
        mask_view: true,
        saveQRCodeDialog: true
      })
      wx.switchTab({
        url: '/pages/index/index',
      })
    }).catch(err=>{
      this.setData({
        mask_view: false,
        saveQRCodeDialog: false
      })
    })

  },
  onShow() {
    let that = this;
    let isLogin = wx.getStorageSync('isLogin') && app.globalData.isLogin;
    if (isLogin){
      utils.getUserDetail(()=>{
        //获取token数据
        let tel = wx.getStorageSync("telNum")
        let phone = tel.substring(0, 3) + "****" + tel.substring(7, 11)
        let name = wx.getStorageSync("userInfo").name
        let img = wx.getStorageSync("userInfo").avatar
        that.setData({
          token: wx.getStorageSync('usertoken'),
          phone: phone,
          name: name,
          headerImg: img,
          isLogin: isLogin
        })
      })
      this.getBabyList()
    }else {
      wx.navigateTo({
        url: '/pages/login/login'
      })
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return{
      title:'宝贝疫苗接种助手',
      path:'/pages/index/index', //这里拼接需要携带的参数
      imageUrl:'/images/appBanner.png',
      success:function(res){
        console.log("转发成功"+res);
      }
    }
  }
})
